﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Print;
using System.Threading;
using VAdvantage.Classes;
using VAdvantage.Utility;

namespace VAdvantage.Process
{
    public partial class ReportProgress : ChildWindow
    {
        ReportEngine _re = null;

        public ReportProgress(ReportEngine re)
        {
            _re = re;
            InitializeComponent();
        }

        String saveFileName = "";
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBlock1.Text.Equals(Msg.GetMsg(Envs.GetCtx(), "ReportFileSaved")))
            {
                this.Close();

            }
            else
            {
                SaveFileDialog d = new SaveFileDialog();
                d.Filter = "PDF file format|*.pdf";

                if (d.ShowDialog() == true)
                {
                    saveFileName = d.SafeFileName;
                    BusyWindow.IsBusy = true;
                    OKButton.IsEnabled = false;

                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        PdfSharp.Pdf.PdfDocument doc = _re.CreatePDF("newfile.pdf");

                        bool saved = false;
                        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate
                        {
                            doc.Save(d.OpenFile());
                            saved = true;

                            BusyWindow.IsBusy = false;
                            OKButton.IsEnabled = true;
                            textBlock1.Text = Msg.GetMsg(Envs.GetCtx(), "ReportFileSaved");
                        });

                        while (!saved)
                            Thread.Sleep(10);


                    });

                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

